package com.ccys.cms.controller.corporateAndInsurance;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitSService;
import com.ccys.cms.service.corporateAndInsurance.PlanBenefitService;
import com.ccys.cms.service.corporateAndInsurance.PlanService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.corporateAndInsurance.PlanDto;
import com.ccys.db.dto.web.productAndService.NumEnum;
import com.ccys.db.entity.TblPlan;
import com.ccys.db.entity.TblPlanBenefit;
import com.ccys.db.rely.BaseEntity;
import com.ccys.db.vo.corporateAndInsurance.PlanVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/corporateAndInsurance")
@Api(tags = "配套管理")
public class PlanController {


    @Resource
    private PlanService service;

    @Resource
    private PlanBenefitService benefitService;

    @Resource
    private PlanBenefitSService benefitSService;


    @PostMapping("plan")
    @ApiOperation("新增/修改")
    @Transactional
    public ResponseResult insert(TblPlan model) {
       return service.change(model);
    }


    @PutMapping("plan")
    @ApiOperation("新增/修改")
    @Transactional
    public ResponseResult update(TblPlan model) {
        return service.change(model);
    }


    @GetMapping("plan/detail")
    @ApiOperation("根据id查询")
    public ResponseResult<TblPlan, Object> getById(String id) {

        return ResponseResult.success(service.findEntityById(id));
    }


    @DeleteMapping("plan")
    @ApiOperation("根据Id删除")
    @Transactional
    public ResponseResult delete(String id) {

        TblPlan model = new TblPlan();
        model.setId(id);
        model.setDelFlag(NumEnum.DEL.getCode());
        int i = service.updateEntityById(model);
        // 删除其他关联的东西
        List<TblPlanBenefit> benefitList = benefitService.findListByLimit(TblPlanBenefit.class, new ArrayList<String>() {{
            add("del_flag = 0");
            add(" plan_id = '" + id + "'");
        }}, "created_on desc");
        // 删除benefit
        benefitService.deleteBenefit(id);
        // 删除benefitService
        List<String> benefitIds = benefitList.stream().map(BaseEntity::getId).collect(Collectors.toList());
        benefitSService.deleteBenefitServiceByBenefitIds(benefitIds);
        return ResponseResult.update(i);
    }


    @GetMapping("plan")
    @ApiOperation(value = "查询分页", notes = "排序字段  t1.status /t2. user_fullname / t1.created_on ")
    public ResponseResult<PageInfo<PlanVo>, Object> getPage(PlanDto dto) {
        return service.getPage(dto);
    }


    @GetMapping("plan/list")
    @ApiOperation(value = "查询集合", notes = "(非必传)type 1:AIA corporate;2:Direct corporate;4:Subscription package")
    public ResponseResult<TblPlan, Object> getList(Integer type) {
        List<TblPlan> planList = service.findListByLimit(TblPlan.class, new ArrayList<String>() {{
            add("del_flag = 0");
            if (type != null) {
                add(" type = " + type);
            }
        }}, "created_on desc");
        return ResponseResult.success(planList);
    }
}
